package com.demo.demo.controller;

import java.util.Date;
import java.util.HashMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.demo.demo.bean.User;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

/**
 * 
 * <p>classname:IndexController</p>
 * <p>Description:</p>
 * <p>Company:斯润天朗</p>
 * @author guogw
 * @version V2.0
 * @date 2017年10月24日 下午6:11:13
 */
@RestController
@RequestMapping(value = "/index")
public class IndexController {
	
	public final Logger logger = LoggerFactory.getLogger(IndexController.class);
	
	@Value(value = "${demo.secret}")
	private String secret;
	
	@Value(value = "${demo.number}")
	private int id;
	
	@Value(value = "${demo.desc}")
	private String desc;

	@ApiIgnore
	@RequestMapping
	public String index() {
		
		logger.debug("this is a log test, debug");
		logger.info("this is a log test, info");
		return "hello world";
	}

	/**
	 * 非restFul 获取用户信息
	 * @param name
	 * @return
	 */
	@ApiIgnore
	@RequestMapping(value = "/getUser/")
	public HashMap<String, Object> getUser(@RequestParam String name) {
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("title", "hello world");
		map.put("name", name);
		map.put("secret", secret);
		map.put("id", id);
		map.put("desc", desc);
		return map;
	}
	
	// @RequestParam 简单类型的绑定，可以出来get和post
	@ApiOperation(value = "获取用户详细信息", notes = "根据用户名获取用户信息")
	@ApiImplicitParam(name ="name", value = "用户名称", required = true, dataType = "String" , paramType = "path")
	@RequestMapping(value = "/get/{name}", method= RequestMethod.GET)
	public HashMap<String, Object> get(@PathVariable String name) {
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("title", "hello world");
		map.put("name", name);
		map.put("secret", secret);
		map.put("id", id);
		map.put("desc", desc);
		return map;
	}

	// @PathVariable 获得请求url中的动态参数
	@RequestMapping(value = "/get/{id}/{name}")
	public User getUser(@PathVariable int id, @PathVariable String name) {
		User user = new User();
		user.setId(id);
		user.setName(name);
		user.setDate(new Date());
		return user;
	}

}
